package com.rcd.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.rcd.dao.IReviewDao;
import com.rcd.exception.MyException;
import com.rcd.util.Page;
import com.rcd.vo.Review;

public class IReviewDaoImpl extends HibernateDaoSupport implements IReviewDao 
{
	public void addReview(Review review) throws MyException 
	{
		getSession().save(review);
	}
	
	public void updateReview(Review review) throws MyException 
	{
		getSession().update(review);
	}
	
	public void delReview(Review review) throws MyException 
	{
		getSession().delete(review);
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public List<Review> findReviewByPid(int pid, Page page) throws MyException 
	{
		Criteria cri = super.getSession().createCriteria(Review.class);
		cri.add(Expression.eq("pid", pid));
		cri.setFirstResult(page.getPagesize() * page.getPagecode() - page.getPagesize());
		cri.setMaxResults(page.getPagesize());
		return cri.list();
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public List<Review> findReviewByUid(int uid, Page page) throws MyException 
	{
		Criteria cri = super.getSession().createCriteria(Review.class);
		cri.add(Expression.eq("uid", uid));
		cri.setFirstResult(page.getPagesize() * page.getPagecode() - page.getPagesize());
		cri.setMaxResults(page.getPagesize());
		return cri.list();
	}

	@SuppressWarnings("unchecked")
	public int getAllSize(Page page) throws MyException 
	{
		int count = 0;
		String hql = "SELECT COUNT(r.rid) FROM Review AS r";
		Query q = this.getSession().createQuery(hql);
		List all = q.list();
		if(all.size() > 0)
		{
			count = ((Long)all.get(0)).intValue();
		}
		return count;
	}
}